package no.uib.fragmentation_analyzer.gui;

/**
 * A simple dialog for selecting the label type to use for the plot.
 *
 * @author Harald Barsnes
 */
public class PlotLabelSelection extends javax.swing.JDialog {

    private FragmentationAnalyzer fragmentationAnalyzer;

    /**
     * Creates a new PlotLabelSelection dialog and makes it visible.
     *
     * @param fragmentationAnalyzer
     * @param modal
     * @param enableMsLimsLabelTypes
     */
    public PlotLabelSelection(FragmentationAnalyzer fragmentationAnalyzer, boolean modal, boolean enableMsLimsLabelTypes) {
        super(fragmentationAnalyzer, modal);

        this.fragmentationAnalyzer = fragmentationAnalyzer;

        initComponents();

        fragmentIonScoringTypeJRadioButton.setEnabled(enableMsLimsLabelTypes);
        fragmentIonThresholdJRadioButton.setEnabled(enableMsLimsLabelTypes);

        // set the current label type
        if(fragmentationAnalyzer.getProperties().getCurrentLabelType()
                == fragmentationAnalyzer.getProperties().PLOT_LABEL_TYPE_INSTRUMENT){
            instrumentNameJRadioButton.setSelected(true);
        } else if(fragmentationAnalyzer.getProperties().getCurrentLabelType()
                == fragmentationAnalyzer.getProperties().PLOT_LABEL_TYPE_FRAGMENT_ION_TYPE){
            fragmentIonTypeJRadioButton.setSelected(true);
        } else if(fragmentationAnalyzer.getProperties().getCurrentLabelType()
                == fragmentationAnalyzer.getProperties().PLOT_LABEL_TYPE_IDENTIFICATION_ID){
            spectrumIdJRadioButton.setSelected(true);
        } else if(fragmentationAnalyzer.getProperties().getCurrentLabelType()
                == fragmentationAnalyzer.getProperties().PLOT_LABEL_TYPE_FRAGMENT_ION_SCORING_TYPE){
            if(fragmentIonScoringTypeJRadioButton.isEnabled()){
                fragmentIonScoringTypeJRadioButton.setSelected(true);
            }
        } else if(fragmentationAnalyzer.getProperties().getCurrentLabelType()
                == fragmentationAnalyzer.getProperties().PLOT_LABEL_TYPE_FRAGMENT_ION_THRESHOLD){
            if(fragmentIonThresholdJRadioButton.isEnabled()){
                fragmentIonThresholdJRadioButton.setSelected(true);
            }
        }

        // enable/disable ok button
        enableOkButton();

        setLocationRelativeTo(fragmentationAnalyzer);
        setVisible(true);
    }

    /**
     * Enables the ok button if one of the radio boxes are selected.
     */
    private void enableOkButton(){
        if(instrumentNameJRadioButton.isSelected() ||
                fragmentIonTypeJRadioButton.isSelected() ||
                spectrumIdJRadioButton.isSelected() ||
                fragmentIonScoringTypeJRadioButton.isSelected() ||
                fragmentIonThresholdJRadioButton.isSelected()){
            okJButton.setEnabled(true);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        backgroundPanel = new javax.swing.JPanel();
        optionsPanel = new javax.swing.JPanel();
        instrumentNameJRadioButton = new javax.swing.JRadioButton();
        fragmentIonTypeJRadioButton = new javax.swing.JRadioButton();
        fragmentIonScoringTypeJRadioButton = new javax.swing.JRadioButton();
        fragmentIonThresholdJRadioButton = new javax.swing.JRadioButton();
        spectrumIdJRadioButton = new javax.swing.JRadioButton();
        okJButton = new javax.swing.JButton();
        cancelJButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select the Data Point Label");
        setResizable(false);

        backgroundPanel.setBackground(new java.awt.Color(230, 230, 230));

        optionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Options"));
        optionsPanel.setOpaque(false);

        buttonGroup.add(instrumentNameJRadioButton);
        instrumentNameJRadioButton.setText("Instrument Name");
        instrumentNameJRadioButton.setToolTipText("<html>\nUse the instrument name as the data<br>\npoint label and and coloring scheme\n</html>");
        instrumentNameJRadioButton.setIconTextGap(15);
        instrumentNameJRadioButton.setOpaque(false);
        instrumentNameJRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                instrumentNameJRadioButtonActionPerformed(evt);
            }
        });

        buttonGroup.add(fragmentIonTypeJRadioButton);
        fragmentIonTypeJRadioButton.setText("Fragment Ion Type");
        fragmentIonTypeJRadioButton.setToolTipText("<html>\nUse the fragment ion type as the data<br>\npoint label and coloring scheme.\n</html>");
        fragmentIonTypeJRadioButton.setIconTextGap(15);
        fragmentIonTypeJRadioButton.setOpaque(false);
        fragmentIonTypeJRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fragmentIonTypeJRadioButtonActionPerformed(evt);
            }
        });

        buttonGroup.add(fragmentIonScoringTypeJRadioButton);
        fragmentIonScoringTypeJRadioButton.setText("Fragment Ion Scoring Type (requires ms_lims)");
        fragmentIonScoringTypeJRadioButton.setToolTipText("<html>\nUse the fragment ion scoring type as the data<br>\npoint label and coloring scheme.\n</html>");
        fragmentIonScoringTypeJRadioButton.setIconTextGap(15);
        fragmentIonScoringTypeJRadioButton.setOpaque(false);
        fragmentIonScoringTypeJRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fragmentIonScoringTypeJRadioButtonActionPerformed(evt);
            }
        });

        buttonGroup.add(fragmentIonThresholdJRadioButton);
        fragmentIonThresholdJRadioButton.setText("Fragment Ion Threshold (requires ms_lims)");
        fragmentIonThresholdJRadioButton.setToolTipText("<html>\nUse the fragment ion threshold as the data<br>\npoint label and coloring scheme.\n</html>");
        fragmentIonThresholdJRadioButton.setIconTextGap(15);
        fragmentIonThresholdJRadioButton.setOpaque(false);
        fragmentIonThresholdJRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fragmentIonThresholdJRadioButtonActionPerformed(evt);
            }
        });

        buttonGroup.add(spectrumIdJRadioButton);
        spectrumIdJRadioButton.setText("Identification ID");
        spectrumIdJRadioButton.setToolTipText("<html>\nUse the identification ID as the data<br>\npoint label and and coloring scheme\n</html>");
        spectrumIdJRadioButton.setIconTextGap(15);
        spectrumIdJRadioButton.setOpaque(false);

        org.jdesktop.layout.GroupLayout optionsPanelLayout = new org.jdesktop.layout.GroupLayout(optionsPanel);
        optionsPanel.setLayout(optionsPanelLayout);
        optionsPanelLayout.setHorizontalGroup(
            optionsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(optionsPanelLayout.createSequentialGroup()
                .add(27, 27, 27)
                .add(optionsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(optionsPanelLayout.createSequentialGroup()
                        .add(optionsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(spectrumIdJRadioButton)
                            .add(fragmentIonTypeJRadioButton)
                            .add(instrumentNameJRadioButton))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 158, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, optionsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(fragmentIonThresholdJRadioButton)
                        .add(fragmentIonScoringTypeJRadioButton)))
                .addContainerGap(77, Short.MAX_VALUE))
        );
        optionsPanelLayout.setVerticalGroup(
            optionsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(optionsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(instrumentNameJRadioButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fragmentIonTypeJRadioButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(spectrumIdJRadioButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fragmentIonScoringTypeJRadioButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fragmentIonThresholdJRadioButton)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        okJButton.setText("OK");
        okJButton.setEnabled(false);
        okJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okJButtonActionPerformed(evt);
            }
        });

        cancelJButton.setText("Cancel");
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout backgroundPanelLayout = new org.jdesktop.layout.GroupLayout(backgroundPanel);
        backgroundPanel.setLayout(backgroundPanelLayout);
        backgroundPanelLayout.setHorizontalGroup(
            backgroundPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(backgroundPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(backgroundPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, optionsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, backgroundPanelLayout.createSequentialGroup()
                        .add(okJButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cancelJButton)))
                .addContainerGap())
        );

        backgroundPanelLayout.linkSize(new java.awt.Component[] {cancelJButton, okJButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        backgroundPanelLayout.setVerticalGroup(
            backgroundPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(backgroundPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(optionsPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(backgroundPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(okJButton)
                    .add(cancelJButton))
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(backgroundPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(backgroundPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Closes the dialog.
     *
     * @param evt
     */
    private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJButtonActionPerformed
        fragmentationAnalyzer.cancelProgress();
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_cancelJButtonActionPerformed

    /**
     * Sends the label type selection to the FragmentationAnalyzer and
     * then closes the dialog.
     *
     * @param evt
     */
    private void okJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okJButtonActionPerformed

        if(instrumentNameJRadioButton.isSelected()){
            fragmentationAnalyzer.getProperties().setCurrentLabelType(
                    fragmentationAnalyzer.getProperties().PLOT_LABEL_TYPE_INSTRUMENT);
        } else if(fragmentIonTypeJRadioButton.isSelected()){
            fragmentationAnalyzer.getProperties().setCurrentLabelType(
                    fragmentationAnalyzer.getProperties().PLOT_LABEL_TYPE_FRAGMENT_ION_TYPE);
        } else if(spectrumIdJRadioButton.isSelected()){
            fragmentationAnalyzer.getProperties().setCurrentLabelType(
                    fragmentationAnalyzer.getProperties().PLOT_LABEL_TYPE_IDENTIFICATION_ID);
        } else if(fragmentIonScoringTypeJRadioButton.isSelected()){
            fragmentationAnalyzer.getProperties().setCurrentLabelType(
                    fragmentationAnalyzer.getProperties().PLOT_LABEL_TYPE_FRAGMENT_ION_SCORING_TYPE);
        } else if(fragmentIonThresholdJRadioButton.isSelected()){
            fragmentationAnalyzer.getProperties().setCurrentLabelType(
                    fragmentationAnalyzer.getProperties().PLOT_LABEL_TYPE_FRAGMENT_ION_THRESHOLD);
        }

        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_okJButtonActionPerformed

    /**
     * @see #enableOkButton()
     */
    private void instrumentNameJRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_instrumentNameJRadioButtonActionPerformed
        enableOkButton();
    }//GEN-LAST:event_instrumentNameJRadioButtonActionPerformed

    /**
     * @see #enableOkButton()
     */
    private void fragmentIonTypeJRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fragmentIonTypeJRadioButtonActionPerformed
        enableOkButton();
    }//GEN-LAST:event_fragmentIonTypeJRadioButtonActionPerformed

    /**
     * @see #enableOkButton()
     */
    private void fragmentIonScoringTypeJRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fragmentIonScoringTypeJRadioButtonActionPerformed
        enableOkButton();
    }//GEN-LAST:event_fragmentIonScoringTypeJRadioButtonActionPerformed

    /**
     * @see #enableOkButton()
     */
    private void fragmentIonThresholdJRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fragmentIonThresholdJRadioButtonActionPerformed
        enableOkButton();
    }//GEN-LAST:event_fragmentIonThresholdJRadioButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel backgroundPanel;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JButton cancelJButton;
    private javax.swing.JRadioButton fragmentIonScoringTypeJRadioButton;
    private javax.swing.JRadioButton fragmentIonThresholdJRadioButton;
    private javax.swing.JRadioButton fragmentIonTypeJRadioButton;
    private javax.swing.JRadioButton instrumentNameJRadioButton;
    private javax.swing.JButton okJButton;
    private javax.swing.JPanel optionsPanel;
    private javax.swing.JRadioButton spectrumIdJRadioButton;
    // End of variables declaration//GEN-END:variables
}
